/*
 * 文件名：PushParkLot.java	 
 * 时     间：下午7:11:21
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.manager.thread;

import com.cloud.otherThreads.UploadReportCarInfoRunble;
import com.cz0735.led.utils.CommondUtil;
import com.cz073586.tools.util.ThreadPoolUtil;
import com.cz073586.tools.util.Tools;
import com.local.common.CommonContains;
import com.local.dao.mapers.CarParkInMapper;
import com.local.dao.mapers.ParklotNumberMapper;
import com.local.entity.vo.ParkwellPayBillVo;
import com.local.entity.vo.ReportCarInfoVo;
import com.local.entity.webpush.CarOutPushVo;
import com.local.manager.cfgManager.reportConfigManager;
import com.local.manager.locks.RecordLockManager;
import com.local.manager.push.PushCarOutBillInfoRunble;
import com.local.manager.push.PushParkLotRunble;
import com.local.model.CarParkIn;
import com.local.model.CarParkOut;
import com.local.model.Screen;
import com.local.service.face.CarInAndOutService;
import com.local.service.face.CrossingService;
import com.local.service.face.LogService;
import com.local.service.face.ParkwellCarOutService;
import com.qianhai.common.utils.Log4jUtil;

/**
 * 类描述:上传到云、推送车位数、推送前端
 * 
 * @ClassName: PushParkLot
 * @author Administrator
 * @date 2015年9月29日 下午7:11:21
 */
public class CarParkOutThread implements Runnable
{
	private static Log4jUtil log = Log4jUtil.getLog4j(CarParkOutThread.class);
	
	private ParkwellCarOutService CarOutService;
	private ParkwellPayBillVo parkwellPayBillVo;
	private CarParkOut carParkOut;
	private CarParkInMapper carParkInMapper;
	private ParklotNumberMapper lotNumberMapper;
	private LogService logService;
	private CarInAndOutService carInAndOutService;
	private CrossingService crossinService;
	
	
	public CarParkOutThread(ParkwellCarOutService carOutService,
			ParkwellPayBillVo parkwellPayBillVo, CarParkOut carParkOut,
			CarParkInMapper carParkInMapper,
			ParklotNumberMapper lotNumberMapper, LogService logService,CarInAndOutService carInAndOutService,CrossingService crossinService)
	{
		super();
		CarOutService = carOutService;
		this.parkwellPayBillVo = parkwellPayBillVo;
		this.carParkOut = carParkOut;
		this.carParkInMapper = carParkInMapper;
		this.lotNumberMapper = lotNumberMapper;
		this.logService = logService;
		this.carInAndOutService = carInAndOutService;
		this.crossinService = crossinService;
	}

	public void setCarOutService(ParkwellCarOutService carOutService)
	{
		CarOutService = carOutService;
	}
	
	public ParkwellPayBillVo getParkwellPayBillVo()
	{
		return parkwellPayBillVo;
	}
	
	public void setParkwellPayBillVo(ParkwellPayBillVo parkwellPayBillVo)
	{
		this.parkwellPayBillVo = parkwellPayBillVo;
	}
	
	public CarParkOut getCarParkOut()
	{
		return carParkOut;
	}
	
	public void setCarParkOut(CarParkOut carParkOut)
	{
		this.carParkOut = carParkOut;
	}
	
	@Override
	public void run()
	{
		
		try
		{
			if(log.isDebugEnabled())
			{
			   log.debug("--->自动开闸线程执行(待解锁),子线程ID"+Thread.currentThread().getId()+"，纪录ID:"+carParkOut.getRecordId() + "，车牌号:"+carParkOut.getCarNo());
			}
			
			CarParkIn carInInfo = carParkInMapper
					.selectByPrimaryKey(parkwellPayBillVo.getCarInUuid());
			if(!Tools.isEmpty(carInInfo))
			{
				RecordLockManager.instance().lock(carInInfo.getRecordId());
			}
			
		
			/************ the end ************/
			CarOutService.carOutNew(parkwellPayBillVo, carParkOut);
			RecordLockManager.instance().removeLock(carParkOut.getRecordId());
			
			
			if(!Tools.isEmpty(carInInfo))
			{
				RecordLockManager.instance().removeLock(carInInfo.getRecordId());
				log.debug("--->解开进场纪录锁(自动开闸出场时),线程ID"+Thread.currentThread().getId()+"，纪录ID："+carInInfo.getRecordId() + "，车牌号:"+carInInfo.getCarNo());
			}
			
			if(log.isDebugEnabled())
			{
				log.debug("--->解开锁(已开闸),线程ID"+Thread.currentThread().getId()+"，纪录ID："+carParkOut.getRecordId() + "，车牌号:"+carParkOut.getCarNo());
			}
			//推送前端页面显示出场信息
			// 推送到前端页面，弹出框
			CarOutPushVo carOutPushVo = new CarOutPushVo();
			carOutPushVo.setCarNo(carParkOut.getCarNo());
			carOutPushVo.setCarNoTye(parkwellPayBillVo.getCarNoTye());
			carOutPushVo.setCarOutUuid(carParkOut.getUuid());
			carOutPushVo.setEuqmentNo(carParkOut.getEquipment());
			carOutPushVo.setIsOpenGate("1");
			// end 出场弹框
			PushCarOutBillInfoRunble pushToWeb = new PushCarOutBillInfoRunble(
					crossinService, carOutPushVo);
			ThreadPoolUtil.instance().execute(pushToWeb);
			
			//上报至云平台，百事通平台等，一些列平台
			CarOutService.uploadPlatform(parkwellPayBillVo, carParkOut,logService);
			
			// 推送总车位数
			PushParkLotRunble lotRunbile = new PushParkLotRunble(carParkInMapper,lotNumberMapper);
			ThreadPoolUtil.instance().execute(lotRunbile);
			
			Screen screen = parkwellPayBillVo.getScreen();
			if(!Tools.isEmpty(screen)&&CommonContains.LIGHT_FLAG.equals(screen.getAttr1())){
				CommondUtil.openSwitch(screen.getScreenIp(),Integer.valueOf(screen.getPortNo()),Integer.valueOf(screen.getScreenNo()));
			}
		} catch (Throwable e)
		{
			log.error("CarParkOutThread", e);
			
			if(log.isDebugEnabled())
			{
			   log.debug("--->解锁不开闸自动出场异常(出)，子线程id"+Thread.currentThread().getId()+",纪录ID"+carParkOut.getRecordId() + "，车牌号:"+carParkOut.getCarNo());
			}
			RecordLockManager.instance().removeLock(carParkOut.getRecordId());
		}
	}
}
